package com.witmore.neutron.ai.mcp.server.trading.controller;

import com.witmore.neutron.ai.mcp.server.trading.dto.StockAlertNotificationDTO;
import com.witmore.neutron.ai.mcp.server.trading.service.WechatNotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信通知测试控制器
 * 用于测试 sendWechatTemplateMessage 方法
 * Author: 豆子高
 * Date: 2025/12/19
 */
@RestController
@RequestMapping("/api/test/wechat")
public class WechatTestController {

    @Autowired
    private WechatNotificationService wechatNotificationService;

    /**
     * 测试发送微信模板消息
     *
     * @param userId 用户ID
     * @return 测试结果
     */
    @PostMapping("/send-template-message/{userId}")
    public Map<String, Object> testSendTemplateMessage(@PathVariable Long userId) {
        Map<String, Object> result = new HashMap<>();

        try {
            // 创建测试用的股票预警数据
            StockAlertNotificationDTO notification = createTestNotification();

            // 调用发送方法
            boolean success = wechatNotificationService.sendStockAlertTemplateMessage(userId, notification);

            result.put("success", success);
            result.put("message", success ? "微信模板消息发送成功" : "微信模板消息发送失败");
            result.put("userId", userId);
            result.put("testData", notification);

        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "发送异常: " + e.getMessage());
            result.put("error", e.getClass().getSimpleName());
        }

        return result;
    }

    /**
     * 创建测试用的股票预警数据
     */
    private StockAlertNotificationDTO createTestNotification() {
        StockAlertNotificationDTO notification = new StockAlertNotificationDTO();
        notification.setStockCode("688256");
        notification.setStockName("寒武纪-U");
        notification.setCurrentPrice(new BigDecimal("12.50"));
        notification.setChangeRatio(new BigDecimal("5.25"));
        notification.setAbnormalType("上涨异动");
        notification.setTriggerThreshold(new BigDecimal("5.00"));
        notification.setAlertTime(LocalDateTime.now());
        notification.setPushFrequency(30);
        notification.setAbnormalRecordId(System.currentTimeMillis());
        return notification;
    }

}
